package com.cy.module;

import com.cy.ext.plugin.route.ControllerBind;
import com.cy.model.Device;
import com.cy.model.DeviceMapTag;
import com.cy.model.DeviceTag;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@ControllerBind(controllerKey = "/clmp/deviceTag")
public class DeviceTagController extends WebApiController {

    @Override
    public void index() {
        DeviceTag deviceTag = getModel(DeviceTag.class);
        int page = getParaToInt("pageIndex");
        int pageSize = getParaToInt("limit");
        Page<DeviceTag> deviceTagPage = DeviceTag.dao.paginate(page, pageSize, DeviceTag.SELECT_ALL, deviceTag);
        for (DeviceTag dt : deviceTagPage.getList()){
            dt.put("creater", dt.getCreater());
            dt.put("devices", dt.getDevices());
        }
        renderBuiPage(deviceTagPage, 200);
    }

    @Before(Tx.class)
    @Override
    public void update() {
        int id = getParaToInt();
        DeviceTag tempDeviceTag = getModel(DeviceTag.class);
        String batchNums = this.getPara("batchNums");
        String[] nums = null;
        if (StringUtils.isNotBlank(batchNums)){
            nums = batchNums.split(",|，");
            List<String> errors = checkBatchNums(nums);
            if (errors.size() > 0){
                renderFail("不存在的设备批次号："+ errors);
                return;
            }
        }

        DeviceTag deviceTag = DeviceTag.dao.findById(id);
        deviceTag.coverExclude(tempDeviceTag, null);
        if (deviceTag.update()) {
            Db.update("delete from device_map_tag where tag_id = ?", deviceTag.getId());
            if (nums != null){
                for (String num : nums){
                    Device device = Device.dao.findByBatchNum(num.trim());
                    new DeviceMapTag().setTagId(deviceTag.getId()).setDeviceId(device.getDeviceId()).save();
                }
            }
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Before(Tx.class)
    @Override
    public void save() {
        DeviceTag deviceTag = getModel(DeviceTag.class);
        String batchNums = this.getPara("batchNums");
        String[] nums = null;
        if (StringUtils.isNotBlank(batchNums)){
            nums = batchNums.split(",|，");
            List<String> errors = checkBatchNums(nums);
            if (errors.size() > 0){
                renderFail("不存在的设备批次号："+ errors);
                return;
            }
        }

        deviceTag.setCreateTime(new Date());
        deviceTag.setCreator(getLoginAccount().getAccountId());
        if (deviceTag.save()) {
            if (nums != null){
                for (String num : nums){
                    Device device = Device.dao.findByBatchNum(num.trim());
                    new DeviceMapTag().setTagId(deviceTag.getId()).setDeviceId(device.getDeviceId()).save();
                }
            }
            renderSuccess();
        } else {
            renderFail();
        }
    }

    @Before(Tx.class)
    @Override
    public void delete() {
        String[] ids = getPara().split("-");
        for (String id : ids) {
            DeviceTag.dao.deleteById(id);
            Db.update("delete from device_map_tag where tag_id = ?", id);
        }
        renderSuccess();
    }

    public void getAll(){
        renderJson(DeviceTag.dao.findAll());
    }

    public void getDevices(){
        DeviceTag model = getModel(DeviceTag.class);
        renderJson(DeviceTag.dao.findById(model.getId()).getDevices());
    }

    private List<String> checkBatchNums(String[] batchNums){
        List<String> inexistent = new ArrayList<>(batchNums.length);
        for (String batchNum : batchNums){
            batchNum = batchNum.trim();
            if (Device.dao.findByBatchNum(batchNum) == null){
                inexistent.add(batchNum);
            }
        }
        return inexistent;
    }
}